﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Input;
using Xpom_Xpum__Integrated_Development_Environment.Model.Controls;
using Xpom_Xpum__Integrated_Development_Environment.View;

namespace Xpom_Xpum__Integrated_Development_Environment.ViewModel.Controls
{
    public class MenuViewModel : ViewModel
    {
        #region Constructor

        /// <summary>
        /// Constructor.
        /// </summary>
        public MenuViewModel()
        {
            if ((bool)(DesignerProperties.IsInDesignModeProperty.GetMetadata(typeof(DependencyObject)).DefaultValue))
            {
            }
            else
            {
                var rfm = JsonConvert.DeserializeObject<RecentFilesModel>(File.ReadAllText("Configuration/Integrated Development Environment/RecentFiles.json"));

                RecentFiles = rfm.RecentFilesList.ToList<string>();

                FileNewClickCommand = new Command(arg => FileNewClickMethod());
                FileOpenClickCommand = new Command(arg => FileOpenClickMethod());
            }
        }

        #endregion

        #region Commands

        /// <summary>
        /// Get or set FileNewClickCommand.
        /// </summary>
        public ICommand FileNewClickCommand { get; set; }

        /// <summary>
        /// Get or set FileOpenClickCommand.
        /// </summary>
        public ICommand FileOpenClickCommand { get; set; }

        #endregion

        #region Fields

        private bool mIsFileOpened;

        private bool mIsProjectOpened;

        private List<string> mRecentFiles;

        #endregion

        #region Properties

        /// <summary>
        /// Is file tab opened in TabControlView.
        /// </summary>
        public bool IsFileOpened
        {
            get
            {
                return mIsFileOpened;
            }
            set
            {
                mIsFileOpened = value;
                OnPropertyChanged("IsFileOpened");
            }
        }

        /// <summary>
        /// Is project opened in IDE.
        /// </summary>
        public bool IsProjectOpened
        {
            get
            {
                return mIsProjectOpened;
            }
            set
            {
                mIsProjectOpened = value;
                OnPropertyChanged("IsProjectOpened");
            }
        }

        /// <summary>
        /// Recent files list.
        /// </summary>
        public List<string> RecentFiles
        {
            get
            {
                return mRecentFiles;
            }
            set
            {
                mRecentFiles = value;
                OnPropertyChanged("RecentFiles");
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// FileNewClickMethod method.
        /// </summary>
        public void FileNewClickMethod()
        {
            var ctw = ((App)App.Current).ctw;
            var MainWindow = Application.Current.MainWindow;
            ctw.Owner = MainWindow;
            if (MainWindow.WindowState == WindowState.Normal)
            {
                ctw.Left = MainWindow.Left + (MainWindow.Width - ctw.Width) / 2;
                ctw.Top = MainWindow.Top + (MainWindow.Height - ctw.Height) / 2;
            }
            else
            {
                var workArea = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea;
                ctw.Left = workArea.Left + (workArea.Width - ctw.Width) / 2;
                ctw.Top = workArea.Top + (workArea.Height - ctw.Height) / 2;
            }
            ctw.ShowDialog();
        }

        /// <summary>
        /// FileOpenClickMethod method.
        /// </summary>
        public void FileOpenClickMethod()
        {
            var fbd = new System.Windows.Forms.FolderBrowserDialog();

            var result = fbd.ShowDialog(((App)App.Current).MainWindow as System.Windows.Forms.IWin32Window);         
            
            if (result == System.Windows.Forms.DialogResult.OK)
            {
                string projectPath = fbd.SelectedPath;

                ((App)App.Current).pevm = new ProjectExplorerViewModel(projectPath);

                IsProjectOpened = true;
            }
        }

        #endregion
    }
}
